package com.atguigu.tingshu.album.api;

import com.atguigu.tingshu.album.service.TrackInfoService;
import com.atguigu.tingshu.common.login.TsLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.album.TrackInfo;
import com.atguigu.tingshu.query.album.TrackInfoQuery;
import com.atguigu.tingshu.vo.album.AlbumTrackListVo;
import com.atguigu.tingshu.vo.album.TrackInfoVo;
import com.atguigu.tingshu.vo.album.TrackListVo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.simpleframework.xml.core.Validate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

@Tag(name = "声音管理")
@RestController
@RequestMapping("api/album/trackInfo")
@SuppressWarnings({"all"})
public class  TrackInfoApiController {

	@Autowired
	private TrackInfoService trackInfoService;

	/**
	 * @author gengjian
	 * @date 2025/02/21 21:19:24
	 * 上传声音
	 */
	@Operation(summary = "上传声音")
	@PostMapping("/uploadTrack")
	public Result uploadTrack(MultipartFile file){
		//调用服务层方法
		Map<String,String> map = trackInfoService.uploadTrack(file);
		//返回声音的访问url
		return Result.ok(map);
	}

	/**
	 * @author gengjian
	 * @date 2025/02/22 14:06:10
	 * 保存声音
	 */
	@TsLogin
	@Operation(summary = "保存声音")
	@PostMapping("/saveTrackInfo")
	public Result saveTrackInfo(@RequestBody @Validate TrackInfoVo trackInfoVo){
		//保存到声音信息表中 还需要用户id
		Long userId = AuthContextHolder.getUserId() == null ? 1l : AuthContextHolder.getUserId();
		//调用服务层方法
		trackInfoService.saveTrackInfo(userId,trackInfoVo);
		return Result.ok();
	}

	/**
	 * @author gengjian
	 * @date 2025/02/22 15:57:52
	 * 分页查询声音列表
	 */
	@TsLogin
	@Operation(summary = "分页查询声音列表")
	@PostMapping("/findUserTrackPage/{pageNum}/{pageSize}")
	public Result findUserTrackPage(@PathVariable("pageNum")int pageNum,
									@PathVariable("pageSize")int pageSize,
									@RequestBody TrackInfoQuery trackInfoQuery){
		Page<TrackListVo> page = new Page<>(pageNum,pageSize);
		Long userId = AuthContextHolder.getUserId()==null? 1l : AuthContextHolder.getUserId();
		trackInfoQuery.setUserId(userId);
		//调用服务层方法
		trackInfoService.findUserTrackPage(page,trackInfoQuery);
		return Result.ok(page);
	}

	/**
	 * @author gengjian
	 * @date 2025/02/23 14:09:44
	 * 根据声音id删除声音
	 */
	@TsLogin
	@Operation(summary = "删除声音")
	@DeleteMapping("/removeTrackInfo/{trackId}")
	public Result removeTrackInfo(@PathVariable("trackId")Long trackId){
		//调用服务层方法
		trackInfoService.removeTrackInfo(trackId);
		return Result.ok();
	}

	/**
	 * @author gengjian
	 * @date 2025/02/23 15:00:24
	 * 点击编辑回显声音信息
	 */
	@Operation(summary = "点击编辑回显声音信息")
	@GetMapping("/getTrackInfo/{trackId}")
	public Result getTrackInfo(@PathVariable("trackId")Long trackId){
		//调用服务层方法
		TrackInfo trackInfo = trackInfoService.getById(trackId);
		return Result.ok(trackInfo);
	}

	/**
	 * @author gengjian
	 * @date 2025/02/23 16:37:13
	 * 修改声音信息
	 */
	@TsLogin
	@Operation(summary = "修改声音信息")
	@PutMapping("/updateTrackInfo/{trackId}")
	public Result updateTrackInfo(@PathVariable("trackId")Long trackId,
								  @RequestBody TrackInfoVo trackInfoVo){
		//调用服务层方法
		trackInfoService.updateTrackInfo(trackId,trackInfoVo);
		return Result.ok();
	}

	/**
	 * @author gengjian
	 * @date 2025/03/03 18:39:18
	 * 根据专辑id分页查询声音列表
	 */
	@TsLogin(requiredLogin = false)//需要用户id，登录和不登录看到的声音列表效果不一样
	@Operation(summary = "根据专辑id分页查询声音列表")
	@GetMapping("findAlbumTrackPage/{albumId}/{pageNum}/{pageSize}")
	public Result findAlbumTrackPage(@PathVariable("albumId")Long albumId,
									 @PathVariable("pageNum")Long pageNum,
									 @PathVariable("pageSize")Long pageSize){
		//获取用户id
		Long userId = AuthContextHolder.getUserId();
		//构建分页对象
		Page<AlbumTrackListVo> page = new Page<>(pageNum,pageSize);
		//调用服务层方法
		trackInfoService.findAlbumTrackPage(page,albumId,userId);
		return Result.ok(page);
	}
}

